package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageListBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.LinkageAdapter;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.List;

import okhttp3.Call;

public class DeviceLinkageActivity extends AppActivity{
    private RecyclerView linkageRecyclerView;
    private Intent intent = null;
    private LinearLayout ll_Noequipment;
    private LinkageAdapter mLinkageAdapter;
    private List<LinkageListBean> allLinkages;
    public String deviceSerialNo;

    @Log
    public static void start(Context context, String serialNo, int deviceGroup) {
        Intent intent = new Intent(context, DeviceLinkageActivity.class);
        intent.putExtra("serialNo", serialNo);
        intent.putExtra("deviceGroup", deviceGroup);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_device_linkage;
    }

    @Override
    protected void initView() {
        linkageRecyclerView = findViewById(R.id.linkage_recycler_view);
        ll_Noequipment = findViewById(R.id.ll_Noequipment);
        initRecyclerView();
    }

    private void initRecyclerView() {
        linkageRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mLinkageAdapter = new LinkageAdapter(getContext());
        linkageRecyclerView.setAdapter(mLinkageAdapter);
        mLinkageAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                LinkageListBean linkage = mLinkageAdapter.getItem(position);
                List<ControllerBean> beans = LLControllerDBManager.getInstance(getContext()).getControllersByProductModel("0x1501");
                if(beans.size() == 0){
                    getLinkageSingleDataNew(linkage.getLinkageSerialNo());
                }else{
                    if(LLSceneLinkageUtil.compareVersion("1.4.0", beans.get(0).getVersion())){
                        getLinkageSingleDataNew(linkage.getLinkageSerialNo());
                    }else{
                        getLinkageSingleData(linkage.getLinkageSerialNo());
                    }
                }
//                LinkageDataHolder.setLinkageData(linkage);
//                setDeviceCustomKeyToEdit(linkage);
            }
        });
        mLinkageAdapter.setOnChildClickListener(R.id.btn_switch, new BaseAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                LinkageListBean linkageBean = mLinkageAdapter.getData().get(position);
                setLinkageDisable(linkageBean.getLinkageSerialNo(), !((SwitchButton) childView).isChecked());
            }
        });
    }

    private void setLinkageDisable(String linkageSerialNo, boolean isDisable) {
        LLHttpManager.setLinkageDisableNew(this, linkageSerialNo, isDisable, new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                toast(R.string.set_success);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initData() {
        if (AppApplication.getInstance().getCurHouseInfo() == null) {
            toast(R.string.house_info_null);
            return;
        }
        deviceSerialNo = getIntent().getStringExtra("serialNo");
        int deviceGroup = getIntent().getIntExtra("deviceGroup",0);
        HttpCallback<HttpData<List<LinkageListBean>>> callback = new HttpCallback<HttpData<List<LinkageListBean>>>(this) {
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onSucceed(HttpData<List<LinkageListBean>> result) {
                allLinkages = result.getData();
                if (allLinkages == null || allLinkages.size() == 0) {
                    ll_Noequipment.setVisibility(View.VISIBLE);
                    linkageRecyclerView.setVisibility(View.GONE);
                } else {
                    ll_Noequipment.setVisibility(View.GONE);
                    linkageRecyclerView.setVisibility(View.VISIBLE);
                    mLinkageAdapter.setData(allLinkages);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        };
        if(deviceGroup == 1){
            LLHttpManager.getLinkageByDeviceGroupSerialNo(this, deviceSerialNo, AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), callback);
        }else{
            LLHttpManager.getLinkageByDeviceSerialNo(this, deviceSerialNo, AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo(), callback);
        }
                //获取全部联动

    }

    private void setDeviceCustomKeyToEdit(LinkageBean linkage){
        Intent intent = new Intent(getContext(), EditLinkageActivity.class);
        intent.putExtra("targetSerialNo",deviceSerialNo);
        startActivity(intent);
    }

    private void getLinkageSingleData(String linkageSerialNo){
        LLHttpManager.getLinkage((LifecycleOwner) getContext(), linkageSerialNo, new HttpCallback<HttpData<LinkageBean>>(null) {
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onSucceed(HttpData<LinkageBean> result) {
                LinkageDataHolder.setLinkageData(result.getData());
                setDeviceCustomKeyToEdit(result.getData());
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }

    private void setDeviceCustomKeyToEditNew(LinkageNewBean linkage){
        Intent intent = new Intent(getContext(), EditLinkageActivityNew.class);
        intent.putExtra("targetSerialNo",deviceSerialNo);
        startActivity(intent);
    }

    private void getLinkageSingleDataNew(String linkageSerialNo){
        LLHttpManager.getLinkageNew((LifecycleOwner) getContext(), linkageSerialNo, new HttpCallback<HttpData<LinkageNewBean>>(null) {
            @Override
            public void onStart(Call call) {
            }

            @Override
            public void onSucceed(HttpData<LinkageNewBean> result) {
                LinkageDataHolder.setLinkageDataNew(result.getData());
                setDeviceCustomKeyToEditNew(result.getData());
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }
}
